<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .ticket-container {
            display: flex;
            margin-bottom: 20px;
            background-color: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 800px; /* 你可以设置一个合适的宽度 */
            height: 200px; /* 你可以设置一个合适的高度 */
            margin: 20px auto;
        }


        .ticket-image {
            width: 30%;
            height: 100%; /* 设置为 100% */
            object-fit: cover;
        }

        .ticket-info {
            position: relative;
            width: 560px;
            padding: 20px;
            box-sizing: border-box; /* 将 box-sizing 属性设置为 border-box */
        }

        .price-wrapper {
            position: absolute;
            bottom: 10px;
            left: 20px;
        }

        .price {
            font-size: 18px;
            font-weight: bold;
        }

        .booking-wrapper {
            position: absolute;
            bottom: 10px;
            right: 20px;
        }

        .booking {
            background-color: #f60;
            color: #fff;
            padding: 10px 20px;
            border-radius: 3px;
            text-decoration: none;
            right: 20px;
        }

        .booking:hover {
            background-color: #ff8c00;
        }

        h2, p {
            margin: 0;
        }

        /* 可以根据需要设置 #tagChoose 的样式 */
        #tagChoose a {
            text-decoration: none;
            color: black;
        }

        #tagChoose div {
            border: 1px solid #ddd;
            padding: 10px;
        }

        /* 可以根据需要设置 #tagChoose 的样式 */
        #locationChoose a {
            text-decoration: none;
            color: black;
        }

        #locationChoose div {
            border: 1px solid #ddd;
            padding: 10px;
        }

        .breadcrumb-item a {
            text-decoration: none;
            color: #3498db;
        }

        .breadcrumb-item button {
            background: none;
            border: none;
            color: #e74c3c;
            cursor: pointer;
            font-size: inherit;
            margin-left: 5px;
            display: inline-block;
        }

        .bar7 form {
            height: 42px;
        }

        .bar7 input {
            width: 250px;
            border-radius: 42px;
            border: 2px solid #324B4E;
            background: #F9F0DA;
            transition: .3s linear;
            float: right;
        }

        .bar7 input:focus {
            width: 300px;
        }

        .bar7 button {
            background: none;
            top: -2px;
            right: 0;
        }

        .bar7 button:before {
            content: "\f002";
            font-family: FontAwesome;
            color: #324b4e;
        }


        form {
            position: relative;
            width: 300px;
            margin: 0 auto;
        }

        input, button {
            border: none;
            outline: none;
        }

        input {
            width: 100%;
            height: 42px;
            padding-left: 13px;
        }

        button {
            height: 42px;
            width: 42px;
            cursor: pointer;
            position: absolute;
        }

        #keyword_input, a {
            display: inline;
        }

        .sort-button {
            display: inline-block;
            padding: 5px 10px;
            background-color: #f1f1f1;
            border: none;
            cursor: pointer;
            position: relative; /* 添加相对定位 */
            width: 150px; /* 设置按钮宽度 */
            height: 30px; /* 设置按钮高度 */
            text-align: center; /* 按钮文本居中 */
            font-size: 14px; /* 设置字体大小 */
        }

        .arrow-up,
        .arrow-down {
            width: 0;
            height: 0;
            display: inline-block;
            border: solid transparent;
            content: "";
            pointer-events: none;
            position: absolute;
        }

        .arrow-up {
            border-top-color: #c21c1c;
            border-width: 6px 4px 0 4px;
            top: 50%; /* 相对父元素垂直居中 */
            transform: translateY(-50%); /* 垂直居中 */
        }

        .arrow-down {
            border-bottom-color: #c21c1c;
            border-width: 0 4px 6px 4px;
            bottom: 50%; /* 相对父元素垂直居中 */
            transform: translateY(50%); /* 垂直居中 */
        }

        .search {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 100px; /* 调整垂直位置 */
        }

        /* 顶部导航栏样式 */
        nav {
            background-color: #fff;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.26);
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            z-index: 99;
            text-align: center; /* 居中显示 */
            display: flex;
            align-items: center;
            padding: 5px 20px;
        }

        nav img {
            height: 30px;
        }

        nav a {
            display: inline-block;
            color: #555;
            text-align: center;
            padding: 10px 60px; /* 调整导航栏每个元素的大小 */
            text-decoration: none;
        }

        nav a:hover {
            color: #000;
            background-color: #f5f5f5;
        }

        nav a.active {
            color: #4caf50;
            background-color: #fff;
        }

        .dropdown {
            position: relative;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            top: 100%;
            width: 150px;
            background-color: #f9f9f9;
            padding: 10px;
            box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
        }

        .dropdown-content a {
            color: black;
            padding: 6px 16px;
            text-decoration: none;
            display: block;
        }

        .dropdown:hover .dropdown-content,
        .dropdown-content:hover {
            display: block;
        }

        /* 顶部导航栏样式 */

        .type-button {
            display: inline-block;
            padding: 5px 10px;
            background-color: #f1f1f1;
            border: none;
            cursor: pointer;
            position: relative; /* 添加相对定位 */
            width: 150px; /* 设置按钮宽度 */
            height: 30px; /* 设置按钮高度 */
            text-align: center; /* 按钮文本居中 */
            font-size: 14px; /* 设置字体大小 */
        }

        .highlight {
            background-color: #ff0; /* 设置高亮时的背景颜色 */
            color: #000000; /* 设置高亮时的文本颜色 */
            /* 添加其他您需要的高亮样式 */
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<nav>
    <!-- logo -->
    <div class="dropdown">
        <a href="http://sight.travel.com/">
            <img src="https://wen-travel.oss-cn-guangzhou.aliyuncs.com/logo.jpg" alt="Logo">
        </a>
    </div>
    <div class="dropdown">
        <a href="http://sight.travel.com/theme" target="_blank">主题精选</a>
    </div>
    <div class="dropdown">
        <a href="http://ticket.travel.com/" target="_blank">去旅行</a>
    </div>
    <div class="dropdown">
        <a href="#">联系我们</a>
    </div>
    <div class="dropdown">
        <a href="http://member.travel.com/sign" target="_blank">签到</a>
    </div>
    <div class="dropdown">
        <a href="#">我的</a>
        <div class="dropdown-content">
            <a href="http://member.travel.com/myAttractionCollection" target="_blank">景点收藏</a>
            <a href="http://member.travel.com/myTicketCollection" target="_blank">票务收藏</a>
            <a href="http://member.travel.com/memberOrder.html" target="_blank">我的订单</a>
            <a href="http://member.travel.com/myCollection" target="_blank">写游记</a>
            <a href="http://member.travel.com/myCollection" target="_blank">我的游记</a>
            <a href="http://member.travel.com/myCollection" target="_blank">我的足迹</a>
            <a href="http://member.travel.com/myCollection" target="_blank">浏览记录</a>
            <a href="http://member.travel.com/myCollection" target="_blank">个人信息</a>
        </div>
    </div>
    <a th:if="${loginUser != null}">欢迎, [[${loginUser.username}]]</a>
    <a th:if="${loginUser == null}" href="http://auth.travel.com/login.html">你好，请登录</a>
    <a th:if="${loginUser == null}" href="http://auth.travel.com/regist.html" class="li_2">免费注册</a>
    <a th:if="${loginUser != null}" href="http://auth.travel.com/loguot.html" class="li_2">退出</a>
</nav>


<!-- 搜索栏 -->
<div class="search">
    <input id="keyword_input" th:value="${result.getKeyword() != null ? result.getKeyword() : result.location}"
           type="text" placeholder="输入目的地..." style="width: 400px; height: 50px;font-size: 18px;"/>
    <img src="/image/search.png" onclick="searchByKeyWord()" style="margin-left: 10px;">
</div>


<!-- 票务类型 -->
<div id="typeButton">
    <button class="type-button" id="全部" onclick="typeButton(this, '全部')">
        <span class="text">全部</span>
    </button>
    <button class="type-button" id="景点门票" onclick="typeButton(this, '景点门票')">
        <span class="text">景点门票</span>
    </button>
    <button class="type-button" id="一日游" onclick="typeButton(this, '一日游')">
        <span class="text">一日游</span>
    </button>
    <button class="type-button" id="多日游" onclick="typeButton(this, '多日游')">
        <span class="text">多日游</span>
    </button>
</div>


<!-- 排序 -->
<div id="buttons">
    <button class="sort-button" data-sort="synthesis" onclick="toggleSort('synthesis', this)">
        <span class="text">综合排序</span>
        <span class="arrow arrow-up"></span>
    </button>
    <button class="sort-button" data-sort="hotScore" onclick="toggleSort('hotScore', this)">
        <span class="text">热度</span>
        <span class="arrow arrow-up"></span>
    </button>
    <button class="sort-button" data-sort="score" onclick="toggleSort('score', this)">
        <span class="text">评分</span>
        <span class="arrow arrow-up"></span>
    </button>
    <button class="sort-button" data-sort="price" onclick="toggleSort('price', this)">
        <span class="text">价格</span>
        <span class="arrow arrow-up"></span>
    </button>
    <button class="sort-button" data-sort="saleCount" onclick="toggleSort('saleCount', this)">
        <span class="text">销量</span>
        <span class="arrow arrow-up"></span>
    </button>
</div>


<!-- 票列表 -->
<div th:each="ticket : ${result.getTickets()}" class="ticket-container"
     th:onclick="'window.open(\'http://ticket.travel.com/' + ${ticket.spuId} + '.html\', \'_blank\')'">
    <img class="ticket-image" th:src="${ticket.image}" alt="景点图片">
    <div class="ticket-info">
        <p><b th:utext="${ticket.spuName}">票务名称</b></p>
        <br>
        <p th:text="${ticket.spuDescription}">票务的信息</p>
        <div class="price-wrapper">
            <span class="currency" style="font-size: 0.5em">￥</span>
            <span class="price" style="font-size: 1.2em;color: #ff6600" th:text="${ticket.price}"></span>
            <span class="starting" style="font-size: 0.5em;margin-right: 10px;">起</span>

            <span class="starting" style="font-size: 0.5em">销量</span>
            <span class="price" style="font-size: 0.5em" th:text="${ticket.saleCount}"></span>
        </div>
        <div class="booking-wrapper">
            <a class="booking" th:href="'http://ticket.travel.com/' + ${ticket.spuId} + '.html'"
               target="_blank">立即预定</a>
        </div>
    </div>
</div>

</body>

<script type="text/javascript">


    // url参数 添加/替换
    function replaceParamVal(url, paramName, replaceVal, forceAdd) {
        var oUrl = url.toString();
        var nUrl;
        if (oUrl.indexOf(paramName) != -1) {
            if (forceAdd) {
                if (oUrl.indexOf("?") != -1) {
                    nUrl = oUrl + "&" + paramName + "=" + replaceVal;
                } else {
                    nUrl = oUrl + "?" + paramName + "=" + replaceVal;
                }
            } else {
                var re = eval('/(' + paramName + '=)([^&]*)/gi');
                nUrl = oUrl.replace(re, paramName + '=' + replaceVal);
            }
        } else {
            if (oUrl.indexOf("?") != -1) {
                nUrl = oUrl + "&" + paramName + "=" + replaceVal;
            } else {
                nUrl = oUrl + "?" + paramName + "=" + replaceVal;
            }
        }
        return nUrl;
    };

    function searchTickets(name, value) {
        //原来的页面的url：location.href
        var url = replaceParamVal(location.href, name, value, true);
        // window.open(url);
        window.location.href = url;
    }

    // 关键词检索
    function searchByKeyWord() {
        var currentKeyword = $("#keyword_input").val();

        // 检查搜索栏是否有关键词
        if (currentKeyword.trim() === '') {
            // 如果搜索栏没有关键词，则跳转至指定 URL
            window.location.href = 'http://search.travel.com/ticketList.html?keyword=';
        } else {
            var currentUrl = location.href;

            // 检查当前 URL 是否包含 keyword 参数
            if (currentUrl.indexOf("keyword") !== -1) {
                // 如果包含，则替换现有的 keyword 参数值
                var newUrl = replaceParamVal(currentUrl, "keyword", currentKeyword, false);
                window.location.href = newUrl;
            } else {
                // 如果不包含，则直接添加 keyword 参数
                var url = replaceParamVal(currentUrl, "keyword", currentKeyword, true);
                window.location.href = url;
            }
        }
    }


    // 获取当前页面的 URL
    var currentURL = window.location.href;

    // 点击触发排序事件
    function toggleSort(sortType, button) {
        const arrow = button.querySelector('.arrow');
        const isArrowUp = arrow.classList.contains('arrow-up');
        const currentSort = getParameterByName('sort');


        if (currentSort && currentSort.startsWith(sortType)) {
            // If the current sort is for the same type, toggle direction
            newSort = currentSort.endsWith('_asc') ? `${sortType}_desc` : `${sortType}_asc`;
        } else {
            // If the current sort is for a different type or not present, set as asc
            newSort = `${sortType}_desc`;
        }

        // Update URL and navigate to the new page
        updateUrlParam('sort', newSort);
        window.location.href = buildNewUrl();

        console.log(`Sorting by ${sortType} ${isArrowUp ? 'descending' : 'ascending'}`);
    }

    function getParameterByName(name) {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(name);
    }

    function updateUrlParam(key, value) {
        const url = new URL(window.location);
        url.searchParams.set(key, value);
        window.history.replaceState({}, '', url);
    }


    function buildNewUrl() {
        const url = new URL(window.location);
        const sortParam = getParameterByName('sort');
        if (sortParam) {
            url.searchParams.set('sort', sortParam);
        } else {
            url.searchParams.delete('sort');
        }
        return url.href;
    }

    // 解析当前 URL 中的排序参数
    function getSortParameter() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('sort');
    }

    // 根据排序参数设置箭头方向
    function setArrowDirection() {
        const sortParameter = getSortParameter();
        const buttons = document.querySelectorAll('.sort-button');

        buttons.forEach(button => {
            const dataSort = button.getAttribute('data-sort');
            const arrow = button.querySelector('.arrow');

            // 判断当前排序参数是否匹配按钮的数据排序值
            console.log(dataSort,arrow)
            if (sortParameter && sortParameter.startsWith(dataSort)) {
                if (sortParameter.endsWith('_asc')) {
                    arrow.classList.remove('arrow-up');
                    arrow.classList.add('arrow-down');
                } else if (sortParameter.endsWith('_desc')) {
                    arrow.classList.remove('arrow-down');
                    arrow.classList.add('arrow-up');
                }
            } else {
                arrow.classList.remove('arrow-down');
                arrow.classList.add('arrow-up');
            }
        });
    }


    //选中的票务类型按钮
    function typeButton(button, type) {
        // 遍历所有按钮，取消高亮
        var buttons = document.getElementsByClassName("sort-button");
        for (var i = 0; i < buttons.length; i++) {
            buttons[i].classList.remove("highlight");
        }

        // 切换票务类型
        switch (type) {
            case "景点门票":
                // 更新URL中的"type"参数并跳转
                var url = new URL(window.location.href);
                url.searchParams.set("type", type);
                window.location.href = url.toString();
                break;
            case "一日游":
                var url = new URL(window.location.href);
                url.searchParams.set("type", type);
                window.location.href = url.toString();
                break;
            case "多日游":
                var url = new URL(window.location.href);
                url.searchParams.set("type", type);
                window.location.href = url.toString();
                break;
            case "全部":
                var url = new URL(window.location.href);
                url.searchParams.delete("type");
                window.location.href = url.toString();
                break;
            default:
                break;
        }
    }

    window.onload = function() {
        // 初始化箭头方向
        setArrowDirection();
    };

    document.addEventListener('DOMContentLoaded', function() {
        console.log("====");
        // 页面加载完成后执行的函数
        highlightButtonBasedOnUrl();

    });

    function highlightButtonBasedOnUrl() {
        const urlParams = new URLSearchParams(window.location.search);
        const typeParam = urlParams.get('type');
        console.log("点击的按钮==="+typeParam);
        if(typeParam == null){ //点击的是全部
            //高亮全部这个按钮
            document.getElementById('全部').classList.add('highlight');
            return;
        }

        // 获取按钮容器
        const buttonContainer = document.getElementById('typeButton');

        // 遍历每个按钮，高亮匹配的按钮
        const buttons = buttonContainer.querySelectorAll('.type-button');
        buttons.forEach(button => {
            const buttonText = button.querySelector('.text').innerText;
            if (typeParam === buttonText) {
                console.log(typeParam+"按钮进行高亮");
                button.classList.add('highlight'); // 添加高亮类，您需要定义该类的样式
            } else {
                button.classList.remove('highlight'); // 移除其他按钮的高亮类
            }
        });
    }




</script>
</html>